package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.Receiver;

import transportDriver.ChatMessage;

public class Action extends TransportReceiver implements ActionListener {
	 

	
	
	public void actionPerformed(ActionEvent e) {     
		
	    
		if(e.getSource() == ClientMain.fenetre.getB1()) {			
					ClientMain.fenetre.getPanel1().setVisible(false);
					ClientMain.UserName=ClientMain.fenetre.getJtf1().getText();			
				    ClientMain.fenetre.getPanel2().setVisible(true);				  							
			}		
		
		
	   else if(e.getSource() == ClientMain.fenetre.getB2()) {
			
					ClientMain.fenetre.getPanel2().setVisible(false);
									    	    		    	    	
			    	try {
			    		
			    		System.setProperty("jgroups.bind_addr", "127.0.0.1" ); 
			    		ClientMain.channel = new JChannel();
				    	ClientMain.channel.setReceiver(this);
				    	ClientMain.channel.connect("ChatCluster");  
				    	
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					    	
			    	ClientMain.fenetre.getPanel3().setVisible(true);			    		
			}	
	
		
	else if(e.getSource() == ClientMain.fenetre.getB3()) {
		
		ChatMessage myChatMessage= new ChatMessage(ClientMain.UserName,ClientMain.fenetre.getJtf3().getText());										
 		Message msg1=new Message();			     						
		msg1.setObject(myChatMessage);							
		           
					try {
																		
								
					ClientMain.channel.send(msg1);
					
		
					} catch (Exception e1) {
						
						e1.printStackTrace();
					}
					
					 
					
			        ClientMain.fenetre.getJtf3().setText(null);			       
		
			}		
		
	
		
     }
}